﻿#region GNU GPL Version 3 License

/// Copyright 2013 Shailesh Lolam
/// 
/// This file Deployment.cs is part of AzEzAuto.
/// 
/// AzEzAuto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
/// 
/// AzEzAuto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/// 
/// You should have received a copy of the GNU General Public License along with AzEzAuto. If not, see http://www.gnu.org/licenses/.
/// 
/// History:
/// ______________________________________________________________
/// Created         04-2013             Shailesh Lolam

#endregion
      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace AzEzAuto.Entities.Requests
{
    [XmlInclude(typeof(Deployment)),
    XmlInclude(typeof(Upgrade)),
    XmlInclude(typeof(Swap)),
    XmlInclude(typeof(ChangeConfig)),
    XmlInclude(typeof(UpdateDeploymentStatus))]
    public abstract class DeploymentBase
    {
        public enum OperationType { Deploy, Upgrade, Swap, Change, UpdateStatus, Delete };
        public abstract OperationType Operation { get; }
        [XmlElement("SN")]
        public string ServiceName;
        [XmlElement("DYN")]
        public string DeploymentName;
        [XmlElement("DYS")]
        public DeploymentSlot DeploymentSlot;
        private List<ExtendedProperty> _properties = new List<ExtendedProperty>();
        [XmlElement("ExP")]
        public List<ExtendedProperty> Properties
        {
            get
            {
                if (_properties == null)
                    _properties = new List<ExtendedProperty>();
                return _properties;
            }
        }
    }

    [XmlRoot("CD")]
    public class Deployment : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.Deploy; } }
        [XmlElement("NM")]
        public string Name;
        [XmlElement("URL")]
        public string PackageUrl;
        [XmlElement("L")]
        public string Label;
        [XmlElement("CFG")]
        public string Configuration;
        [XmlElement("SD")]
        public bool StartDeployment;
        [XmlElement("TWE")]
        public bool TreatWarningsAsError;
        [XmlElement("ST")]
        public string SwapTo;
        [XmlElement("EXTNCFG")]
        public ExtensionConfiguration ExtensionConfiguration;

    }

    [XmlRoot("UD")]
    public class Upgrade : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.Upgrade; } }
        [XmlElement("MD")]
        public Mode Mode;
        [XmlElement("URL")]
        public string PackageUrl;
        [XmlElement("CFG")]
        public string Configuration;
        [XmlElement("L")]
        public string Label;
        [XmlElement("RL")]
        public string RoleToUpgrade;
        [XmlElement("F")]
        public bool Force;
    }

    [XmlRoot("SWD")]
    public class Swap : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.Swap; } }
        [XmlElement("PD")]
        public string Production;
        [XmlElement("SD")]
        public string SourceDeployment;
    }

    [XmlRoot("CDC")]
    public class ChangeConfig : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.Change; } }
        [XmlElement("CFG")]
        public string Configuration;
        [XmlElement("TWE")]
        public bool TreatWarningsAsError;
        [XmlElement("MD")]
        public Mode Mode;
    }


    [XmlRoot("UDS")]
    public class UpdateDeploymentStatus : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.UpdateStatus; } }
        [XmlElement("DS")]
        public DeploymentStatus Status;
    }


    public class DeleteDeployment : DeploymentBase
    {
        public override OperationType Operation { get { return OperationType.Delete; } }
    }


    [XmlRoot("EXTNCFG")]
    public class ExtensionConfiguration
    {
        private List<Extension> _allRoles;

        [XmlArray("ARLS")]
        [XmlArrayItem("Ext", Type = typeof(Extension))]
        public List<Extension> AllRoles
        {
            get
            {
                if (_allRoles == null)
                    _allRoles = new List<Extension>();
                return _allRoles;
            }
        }
        public bool ShouldSerializeAllRoles() { return _allRoles != null && _allRoles.Count > 0; }

        private List<ExtRole> _namedRoles;

        [XmlArray("NMDRLS")]
        [XmlArrayItem("RL", Type=typeof(ExtRole))]
        public List<ExtRole> NamedRoles
        {
            get
            {
                if (_namedRoles == null)
                    _namedRoles = new List<ExtRole>();
                return _namedRoles;
            }
        }
        public bool ShouldSerializeNamedRoles() { return _namedRoles != null && _namedRoles.Count > 0; }
    }

    [XmlRoot("RL")]
    public class ExtRole
    {
        [XmlElement("RLNM")]
        public string RoleName;

        private List<Extension> _extension;

        [XmlArray("EXTNS")]
        [XmlArrayItem("EXTN", Type = typeof(Extension))]
        public List<Extension> Extensions
        {
            get
            {
                if (_extension == null)
                    _extension = new List<Extension>();
                return _extension;
            }
        }
    }

    [XmlRoot("EXTN")]
    public class Extension
    {
        [XmlElement("Id")]
        public string Id;
    }
}
